package com.yt.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.order.models.service.IWorkOrderInfoService;
import com.yt.order.models.bo.WorkOrderInfoBO;
import com.yt.order.models.dto.WorkOrderInfoDTO;

/**
 * 工单Api控制器（客户端接口）
 *
 * @author xieyanfei
 * @Date 2025-09-04 21:53:06
 */
@RestController
@RequestMapping("/api/workOrderInfo")
@Slf4j
@Api(value = "WorkOrderInfoApiController", tags = "工单Api接口")
public class WorkOrderInfoApiController {

    @DubboReference
    private IWorkOrderInfoService workOrderInfoService;

    /**
     * 根据ID查询工单详情
     * @param id 工单ID
     * @return 工单详情DTO
     */
    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据工单ID查询详细信息")
    @ApiImplicitParam(name = "id", value = "工单ID", required = true, paramType = "query", dataType = "Long")
    public Result<WorkOrderInfoDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002); // 参数错误
        }
        WorkOrderInfoDTO dto = workOrderInfoService.selectById(id);
        return new Result<>(dto);
    }

    /**
     * 分页查询工单列表（支持筛选）
     * @param bo 包含分页参数和筛选条件的BO对象
     * @return 分页后的工单列表
     */
    @ApiOperation(value = "工单列表", notes = "分页查询工单列表，支持按状态、时间范围筛选")
    @PostMapping("/queryPageList")
    public Result<Page<WorkOrderInfoDTO>> queryPageList(@RequestBody WorkOrderInfoBO bo) {
        // 构建查询条件
        Wrapper<WorkOrderInfoDTO> wrapper = new EntityWrapper<>();

        // 状态筛选
        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }

        // 时间范围筛选（创建时间）
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());
        }

        // 分页查询（默认第一页，每页10条，若BO中未指定则使用MyBatis-Plus默认值）
        Page<WorkOrderInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = workOrderInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}